'use strict';

angular.module('afasApp')
  .factory('principal', ['$q', '$http', '$timeout', 'ServicesLS',
  function($q, $http, $timeout, ServicesLS) {
    var _identity = {},
        _authenticated = false;

    var name = ServicesLS.get('_name');
    var roles = ServicesLS.get('_roles');
    // console.log('');
    // roles = JSON.parse(roles);

    if ((typeof name !== 'undefined' && name !== null) && (typeof roles !== 'undefined' && roles.length > 0 )) {
      _identity.name = name;
      _identity.roles = roles;
      _authenticated = true;
    };

    return {
      isIdentityResolved: function() {
        return angular.isDefined(_identity);
      },
      isAuthenticated: function() {
        return _authenticated;
      },
      isInRole: function(roles) {
        if (!_authenticated || !_identity.roles) {
          return false;
        }

        return _identity.roles.indexOf(roles) !== -1;
      },
      isInAnyRole: function(roles) {
        // console.log(_identity);
        if (!_authenticated || !_identity.roles) {
          return false;
        }

        for (var i = 0; i < roles.length; i++) {
          if (this.isInRole(roles[i])) {
            return true;
          }
        }

        return false;
      },
      authenticate: function(identity) {
        _identity = identity;
        // _authenticated = identity !== null;
        _authenticated = true;
      },
      identity: function(force) {
        var deferred = $q.defer();

        if (force === true) {
          _identity = undefined;
        }

        // check and see if we have retrieved the identity data from the server. if we have, reuse it by immediately resolving
        if (angular.isDefined(_identity)) {
          deferred.resolve(_identity);

          return deferred.promise;
        }

        // otherwise, retrieve the identity data from the server, update the identity object, and then resolve.
        //                   $http.get('/svc/account/identity', { ignoreErrors: true })
        //                        .success(function(data) {
        //                            _identity = data;
        //                            _authenticated = true;
        //                            deferred.resolve(_identity);
        //                        })
        //                        .error(function () {
        //                            _identity = null;
        //                            _authenticated = false;
        //                            deferred.resolve(_identity);
        //                        });

        // for the sake of the demo, fake the lookup by using a timeout to create a valid
        // fake identity. in reality,  you'll want something more like the $http request
        // commented out above. in this example, we fake looking up to find the user is
        // not logged in

        // var self = this;
        // $timeout(function() {
        //   self.authenticate(null);
        //   deferred.resolve(_identity);
        // }, 1000);
        deferred.resolve(_identity);

        return deferred.promise;
      }
    };
  }
]);
